//index.js
const app = getApp()

 const Api = require("../../utils/api.js").API
 const Store = require("../../utils/store.js").store

Page({
  data: {
      list:[]
  },
  onLoad(){
      let that = this;
          var bean = {};
          const eventChannel = this.getOpenerEventChannel();
          eventChannel.on('acceptDataFromOpenerPage', function(data) {
              bean = data.data;
              let dat ={
              	"asc": true,
              	"goodsNo": bean,
              	"limit": 10,
              	"page": 1,
              	"popularizeContent": "",
              	"popularizeNo": "",
              	"popularizeTitle": "",
              	"popularizeType": "",
              	"sidx": ""
              }
              
                    Api.popularizeApi(dat).then(res=>{
                        that.setData({
                            list:res.data.records,
                        })      
                    })
             
       
             
      
          })
      
  },
  onShow(){
      
    
     
  },
  fuzhi(e){
       let a = e.currentTarget.dataset.item;
       wx.setClipboardData({  data:a,})
  },
  imgYu:function (e) {
      var that=this;
      let a = e.currentTarget.dataset.item;
      // var src = that.data.discount.imgPath;//获取data-src
      var imgList = [a];//获取data-list
      //图片预览
      wx.previewImage({
        current: a, // 当前显示图片的http链接
        urls: imgList // 需要预览的图片http链接列表
      })
      },
      /**
         * 下载图标到相册
         */
        saveToPhone:function(e){
       let arr = e.currentTarget.dataset.item;
       arr.forEach(ite=>{
           let imgSrc = ite.attUrl; 
           console.log(imgSrc)
           wx.showLoading({
             title: '保存中...'
           })
           wx.downloadFile({  
             url: imgSrc,
             success: function (res) {
               console.log(res);
               //图片保存到本地
               wx.saveImageToPhotosAlbum({
                 filePath: res.tempFilePath,
                 success: function (data) {
                   console.log(data)
                   wx.hideLoading()
                   wx.showToast({
                     title: '保存成功',
                     icon: 'success',
                     duration: 2000
                   })
                 },
                 fail: function (err) {
                   console.log(err);
                   // $yjpToast.show({
                   //   text: `保存失败`
                   // })
                   if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
                     console.log("当初用户拒绝，再次发起授权")
                     wx.showModal({
                       title: '提示',
                       content: '需要您授权保存相册',
                       showCancel: false,
                       success: modalSuccess => {
                         wx.openSetting({
                           success(settingdata) {
                             console.log("settingdata", settingdata)
                             if (settingdata.authSetting['scope.writePhotosAlbum']) {
                               wx.showModal({
                                 title: '提示',
                                 content: '获取权限成功,再次点击图片即可保存',
                                 showCancel: false,
                               })
                             } else {
                               wx.showModal({
                                 title: '提示',
                                 content: '获取权限失败，将无法保存到相册哦~',
                                 showCancel: false,
                               })
                             }
                           },
                           fail(failData) {
                             console.log("failData", failData)
                           },
                           complete(finishData) {
                             console.log("finishData", finishData)
                           }
                         })
                       }
                     })
                   }
                 },
                 complete(res) {
                   console.log(res);
                   wx.hideLoading()
                 }
               })
                 
                 
             }  
                 
           })
                 
           
       })
     
          //获取相册授权
    
      
      
      
      
      
        },
        
        handleDownload(e) {
             // let arr = e.currentTarget.dataset.item[0];
            let link = e.currentTarget.dataset.item[0];
            let fileName = new Date().valueOf();
            wx.downloadFile({
              url: link,
              filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
              success: res => {
                console.log(res);
                let filePath = res.filePath;
                wx.saveVideoToPhotosAlbum({
                  filePath,
                  success: file => {
                    wx.showToast({
                      title: '下载成功',
                      icon: 'success',
                      duration: 2000
                    })
                    let fileMgr = wx.getFileSystemManager();
                    fileMgr.unlink({
                      filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
                      success: function (r) {
        
                      },
                    })
                  },
                  fail: err => {
                    console.log(err)
                    if (err.errMsg === 'saveVideoToPhotosAlbum:fail auth deny') {
                      wx.showModal({
                        title: '提示',
                        content: '需要您授权保存相册',
                        showCancel: false,
                        success: data => {
                          wx.openSetting({
                            success(settingdata) {
                              if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                wx.showModal({
                                  title: '提示',
                                  content: '获取权限成功,再次点击下载即可保存',
                                  showCancel: false,
                                })
                              } else {
                                wx.showModal({
                                  title: '提示',
                                  content: '获取权限失败，将无法保存到相册哦~',
                                  showCancel: false,
                                })
                              }
                            },
                          })
                        }
                      })
                    }
                  }
                })
              }
            })
          },
        

  
  
  

 })